<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Terrain.pb</title></head>
<body bgcolor="#FFFFDF" link="#009999" vlink="#006666" alink="#006666">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font><font color="#006666">;   PureBasic - Terrain : Create 9 terrains in one TerrainGroup
</font><font color="#006666">;
</font><font color="#006666">;    (c) Fantaisie Software
</font><font color="#006666">;
</font><font color="#006666">; ------------------------------------------------------------
</font><font color="#006666">;
</font><font color="#006666">; It can take few minutes to build all terrains (it will be more faster after saving it)
</font><font color="#006666">MessageRequester</font>("Warning !", "It can take a few minutes to build all terrains...", 0) 

<b><font color="#006666">IncludeFile</font></b> <font color="#924B72">#PB_Compiler_Home</font> + "examples/3d/Screen3DRequester.pb"

<font color="#924B72">#CameraSpeed</font> = 2
<font color="#924B72">#TerrainMiniX</font> = -1
<font color="#924B72">#TerrainMiniY</font> = -1
<font color="#924B72">#TerrainMaxiX</font> =  1
<font color="#924B72">#TerrainMaxiY</font> =  1

<b><font color="#006666">Define</font></b>.f KeyX, KeyY, MouseX, MouseY
<b><font color="#006666">Declare</font></b> <font color="#006666">InitBlendMaps</font>()

<font color="#006666">; OpenGL needs to have CG enabled to work (Linux and OS X have OpenGL by default)
</font><font color="#006666">;
</font><b><font color="#006666">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> &lt;&gt; <font color="#924B72">#PB_OS_Windows</font> <b><font color="#006666">Or</font></b> <font color="#006666">Subsystem</font>("OpenGL")
  Flags = <font color="#924B72">#PB_Engine3D_EnableCG</font>
<b><font color="#006666">CompilerEndIf</font></b>

<b><font color="#006666">If</font></b> <font color="#006666">InitEngine3D</font>(Flags)
  
<font color="#006666">  InitSprite</font>()
<font color="#006666">  InitKeyboard</font>()
<font color="#006666">  InitMouse</font>()
  
  <b><font color="#006666">If</font></b> <font color="#006666">Screen3DRequester</font>()
<font color="#006666">    Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + "examples/3d/Data/Textures/", <font color="#924B72">#PB_3DArchive_FileSystem</font>)
<font color="#006666">    Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + "examples/3d/Data/Textures/nvidia", <font color="#924B72">#PB_3DArchive_FileSystem</font>)
<font color="#006666">    Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + "examples/3d/Data/Packs/desert.zip", <font color="#924B72">#PB_3DArchive_Zip</font>)
    
    <font color="#006666">;- Light 
</font>    <font color="#006666">;
</font>    light =<font color="#006666"> CreateLight</font>(<font color="#924B72">#PB_Any</font> ,<font color="#006666">RGB</font>(190, 190, 190), 4000, 1200, 1000,<font color="#924B72">#PB_Light_Directional</font>)
<font color="#006666">    SetLightColor</font>(light, <font color="#924B72">#PB_Light_SpecularColor</font>,<font color="#006666"> RGB</font>(255*0.4, 255*0.4,255*0.4)) 
<font color="#006666">    LightDirection</font>(light ,0.55, -0.3, -0.75) 
<font color="#006666">    AmbientColor</font>(<font color="#006666">RGB</font>(255*0.2, 255*0.2,255*0.2))
    
    <font color="#006666">;- Camera 
</font>    <font color="#006666">;
</font><font color="#006666">    CreateCamera</font>(0, 0, 0, 100, 100)
<font color="#006666">    MoveCamera</font>(0,  800, 400, 80, <font color="#924B72">#PB_Absolute</font>)
<font color="#006666">    CameraBackColor</font>(0,<font color="#006666"> RGB</font>(5, 5, 10))
    
    
    <font color="#006666">;- Terrain definition
</font><font color="#006666">    SetupTerrains</font>(<font color="#006666">LightID</font>(Light), 3000, <font color="#924B72">#PB_Terrain_NormalMapping</font>)
    <font color="#006666">; initialize terrain
</font><font color="#006666">    CreateTerrain</font>(0, 513, 12000, 600, 3, "TerrainGroup", "dat")
    <font color="#006666">; set all texture will be use when terrrain will be constructed 
</font><font color="#006666">    AddTerrainTexture</font>(0,  0, 100, "dirt_grayrocky_diffusespecular.jpg",  "dirt_grayrocky_normalheight.jpg")
<font color="#006666">    AddTerrainTexture</font>(0,  1,  30, "grass_green-01_diffusespecular.jpg", "grass_green-01_normalheight.jpg")
<font color="#006666">    AddTerrainTexture</font>(0,  2, 200, "growth_weirdfungus-03_diffusespecular.jpg", "growth_weirdfungus-03_normalheight.jpg")
    
    <font color="#006666">; Build terrains
</font>    <b><font color="#006666">For</font></b> ty = <font color="#924B72">#TerrainMiniY</font> <b><font color="#006666">To</font></b> <font color="#924B72">#TerrainMaxiY</font>
      <b><font color="#006666">For</font></b> tx = <font color="#924B72">#TerrainMiniX</font> <b><font color="#006666">To</font></b> <font color="#924B72">#TerrainMaxiX</font>
        Imported =<font color="#006666"> DefineTerrainTile</font>(0, tx, ty, "terrain513.png", ty % 2, tx % 2)  
      <b><font color="#006666">Next</font></b>
    <b><font color="#006666">Next</font></b>  
<font color="#006666">    BuildTerrain</font>(0)  
    
    <b><font color="#006666">If</font></b> imported = <font color="#924B72">#True</font>
<font color="#006666">      InitBlendMaps</font>() 
<font color="#006666">      UpdateTerrain</font>(0)
      
      <font color="#006666">; If enabled, it will save the terrain as a (big) cache for a faster load next time the program is executed
</font>      <font color="#006666">; SaveTerrain(0, #False)
</font>    <b><font color="#006666">EndIf</font></b>  
    
    <font color="#006666">; SkyBox
</font>    <font color="#006666">;
</font><font color="#006666">    SkyBox</font>("desert07.jpg")
    
    <b><font color="#006666">Repeat</font></b>
<font color="#006666">      Screen3DEvents</font>()
      
      <b><font color="#006666">If</font></b> <font color="#006666">ExamineKeyboard</font>()       
        
        <b><font color="#006666">If</font></b> <font color="#006666">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Left</font>)
          KeyX = -<font color="#924B72">#CameraSpeed</font> 
        <b><font color="#006666">ElseIf</font></b> <font color="#006666">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Right</font>)
          KeyX = <font color="#924B72">#CameraSpeed</font> 
        <b><font color="#006666">Else</font></b>
          KeyX = 0
        <b><font color="#006666">EndIf</font></b>
        
        <b><font color="#006666">If</font></b> <font color="#006666">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Up</font>)
          KeyY = -<font color="#924B72">#CameraSpeed</font> 
        <b><font color="#006666">ElseIf</font></b> <font color="#006666">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Down</font>)
          KeyY = <font color="#924B72">#CameraSpeed</font> 
        <b><font color="#006666">Else</font></b>
          KeyY = 0
        <b><font color="#006666">EndIf</font></b>
        
      <b><font color="#006666">EndIf</font></b>
      
      <b><font color="#006666">If</font></b> <font color="#006666">ExamineMouse</font>()
        MouseX = -<font color="#006666">MouseDeltaX</font>() * <font color="#924B72">#CameraSpeed</font> * 0.05
        MouseY = -<font color="#006666">MouseDeltaY</font>() * <font color="#924B72">#CameraSpeed</font> * 0.05
      <b><font color="#006666">EndIf</font></b>
<font color="#006666">      MoveCamera</font>(0,<font color="#006666"> CameraX</font>(0),<font color="#006666"> TerrainHeight</font>(0,<font color="#006666"> CameraX</font>(0),<font color="#006666"> CameraZ</font>(0)) + 20,<font color="#006666"> CameraZ</font>(0), <font color="#924B72">#PB_Absolute</font>)
<font color="#006666">      MoveCamera  </font>(0, KeyX, 0, KeyY)
<font color="#006666">      RotateCamera</font>(0,  MouseY, MouseX, 0, <font color="#924B72">#PB_Relative</font>)  
      
<font color="#006666">      RenderWorld</font>()
<font color="#006666">      FlipBuffers</font>()
      
    <b><font color="#006666">Until</font></b> <font color="#006666">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Escape</font>)   
    
    <b><font color="#006666">End</font></b> 
    
  <b><font color="#006666">EndIf</font></b> 
<b><font color="#006666">Else</font></b>
  <b><font color="#006666">CompilerIf</font></b> <font color="#924B72">#PB_Compiler_OS</font> &lt;&gt; <font color="#924B72">#PB_OS_Windows</font> <b><font color="#006666">Or</font></b> <font color="#006666">Subsystem</font>("OpenGL")
    <font color="#006666">;
</font>    <font color="#006666">; Terrain on Linux/OSX and Windows with OpenGL needs CG toolkit from nvidia
</font>    <font color="#006666">; It can be freely downloaded and installed from this site: https://developer.nvidia.com/cg-toolkit-download
</font>    <font color="#006666">;
</font><font color="#006666">    MessageRequester</font>("Error","Can't initialize engine3D (Please ensures than CG Toolkit from nvidia is correcly installed)")
  <b><font color="#006666">CompilerElse</font></b>
<font color="#006666">    MessageRequester</font>("Error","Can't initialize engine3D")
  <b><font color="#006666">CompilerEndIf</font></b>
<b><font color="#006666">EndIf</font></b> 

<b><font color="#006666">Procedure</font></b> <font color="#006666">Clamp</font>(*var.float, min.f, max.f)
  <b><font color="#006666">If</font></b> *var\f &lt; min
    *var\f = min
  <b><font color="#006666">ElseIf</font></b> *var\f &gt; max
    *var\f = max
  <b><font color="#006666">EndIf</font></b>
<b><font color="#006666">EndProcedure</font></b>  

<b><font color="#006666">Procedure</font></b> <font color="#006666">InitBlendMaps</font>()
  minHeight1.f = 70
  fadeDist1.f = 40
  minHeight2.f = 70
  fadeDist2.f = 15    
  <b><font color="#006666">For</font></b> ty = <font color="#924B72">#TerrainMiniY</font> <b><font color="#006666">To</font></b> <font color="#924B72">#TerrainMaxiY</font>
    <b><font color="#006666">For</font></b> tx = <font color="#924B72">#TerrainMiniX</font> <b><font color="#006666">To</font></b> <font color="#924B72">#TerrainMaxiX</font>
      Size =<font color="#006666"> TerrainTileLayerMapSize</font>(0, tx, ty)
      <b><font color="#006666">For</font></b> y = 0 <b><font color="#006666">To</font></b> Size-1
        <b><font color="#006666">For</font></b> x = 0 <b><font color="#006666">To</font></b> Size-1
          Height.f =<font color="#006666"> TerrainTileHeightAtPosition</font>(0, tx, ty, 1, x, y)
          
          val.f = (Height - minHeight1) / fadeDist1
<font color="#006666">          Clamp</font>(@val, 0, 1)
<font color="#006666">          SetTerrainTileLayerBlend</font>(0, tx, ty, 1, x, y, val)
          
          val.f = (Height - minHeight2) / fadeDist2
<font color="#006666">          Clamp</font>(@val, 0, 1)
<font color="#006666">          SetTerrainTileLayerBlend</font>(0, tx, ty, 2, x, y, val)
        <b><font color="#006666">Next</font></b>
      <b><font color="#006666">Next</font></b>
<font color="#006666">      UpdateTerrainTileLayerBlend</font>(0, tx, ty, 1)
<font color="#006666">      UpdateTerrainTileLayerBlend</font>(0, tx, ty, 2)
    <b><font color="#006666">Next</font></b>
  <b><font color="#006666">Next</font></b>  
<b><font color="#006666">EndProcedure</font></b>  
</font></pre>
</body></html>
